﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EnglishWordsAnalyster
{
    public class SpellConverter
    {
        #region 拼音码常量

        public const string py_second_py = "cjwgnspgcenegypbtwxzdxykygtzjnmjqmbsgzscyjsyyfpggbzgydywjkgaljswkbjqhyjwrdzlsgmrybywwccgznkydgttngjeyekzy" +
       "dcjnmcylqlypyqbqrpzslwbdgkjfyxjwcltbncxjjjjcxdtqsqzycdxxhgckbphffsstybgmxjbbyglbhlssmzmyjhsojnghdzcdklgjh" +
       "sgqzhxqgkezzwymcscjnyetxadzpmdssmzjjqjyzcjjfwqjbdzbjgdnzcbwhgxhqkmwfbpbqdtjjzkqhylcgxfptyjyyzpsjlfchmqshg" +
       "mmxsxjykdcmbbqbefsjwhwwgckpylqbgldlcctnmaeddksjngkcsgxlhzaybdbtsdkdylhgymylcxpycjndqjwxqxfyyfjlejbzrwccqh" +
       "qcsbzkymgplbmcrqcflnymyqmsqtrbcjthztqfrxchxmcjcjlxqgjmshzkbswxemdlckfsydsglycjjssjnqbjctyhbftdcyjdgwyghqf" +
       "rxwckqkxebpdjpxjqsrmebwgjlbjslyysmdxlclqkxlhtjrjjmbjhxhwywcbhtrxxglhjhfbmgykldyxzpwlggsmtcbbajjzyljtyanjg" +
       "bjflqgdzyqcaxbkclecjsznslyzhlxlzcghbxzhznytdsbcjkdlzayffydlabbgqszkggldndnyskjshdlxxbcghxyggdjmmzngmmccgw" +
       "zszxsjbznmlzdthcqydbdllscddnlkjyhjsycjlkohqasdhnhcsgaehdaashtcplcpqybsdmpjlpcjaqlcdhjjasprchngjnlhlyyqyhw" +
       "zpnccgwwmzffjqqqqxxaclbhkdjxdgmmydjxzllsygxgkjrywzwyclzmcsjzldbndcfcxyhlschycjqsqqagmnyxpfrkssbjlyxyjjgln" +
       "scmhcwwmnzjjlhmhchsyzsttxrycsxbyhcsmxjsxnbwgpxxtaybgajcxlyxdccwqocwkccsbnhcpdyznbcyytyckskybsqkkytqqxfcwc" +
       "hcwkelcqbsqyjqcclmthsywhmktlkjlychwheqjhtjhzpqsqscfymmcmgbmhglgsllysdllljpchmjhwljcyhzjxhdxjlhxrswlwzjcbx" +
       "mhzqxsdzpmgfcsglsdymjshxpjxomyqknmyblrthbcftpmgyxlchlhlzylxgsssscclsldclepbhshxyyfhbmgdfycnjqwlqhjjcywjzt" +
       "ejjdhfblqxtqkwhdchqxagtlxljxmsljhdzkzjecxjcjnmbbjcsfywkbjzghysdcpqyrsljpclpwxsdwejbjcbcnaytmgmbapclyqbclz" +
       "xcbnmsggfnzjjbzsfqyndxhpcqkzczwalsbccjxsozgwkybsgxfcfcdkhjbstlqfsgdslqwzkxtmhsbgzhjcrglyjbpmljsxlcjqqhzmj" +
       "czydjwbmjklddpmjegxyhylxhlqyqhkycwcjmyhxnatjhyccxzpcqlbzwwwtwbqcmlbmynjcccxbbsnzzljpljxyztzlgcldcklyrzzgq" +
       "tgjhhgjljaxfgfjzslcfdqzlclgjdjcsnclljpjqdcclcjxmyzftsxgcgsbrzxjqqcczhgyjdjqqlzxjyldlbcyamcstylbdjbyregklz" +
       "dzhldszchznwczcllwjqjjjkdgjcolbbzppglghtgzcygezmycnqcycyhbhgxkamtxyxnbskyzzgjzlqjdfcjxdygjqjjpmgwgjjjpkjs" +
       "bgbmmcjssclpqpdxcdyykypcjddyygywchjrtgcnyqldkljczzgzccjgdyksgpzmdlcphnjafyzdjcnmwescsglbtzcgmsdllyxqsxsbl" +
       "jsbbsgghfjlwpmzjnlyywdqshzxtyywhmcyhywdbxbtlmswyyfsbjcbdxxlhjhfssxzqhfzmqcztqcxzxrdkdjhnnyzqqfnqdmmgnydxm" +
       "jgdhcdycbffallztdltfkmxqzdngeqdbdczjdxbzgsqqddjcmbkxffxmkdmcsychzcmljdjynhprsjmkmpcklgdbqtfzswtfgglyplljz" +
       "hgjjgypzltcsmcnbtjbhfkdhbyzgkpbbymtdlsxsbnpdkleycjnycdykzddhqgsdzsctarlltkzlgecllkjljjaqnbdggghfjtzqjsecs" +
       "halqfmmgjnlyjbbtmlycxdcjpldlpcqdhsycbzsckbzmsljflhrbjsnbrgjhxpdgdjybzgdlgcsezgxlblgyxtwmabchecmwyjyzlljjs" +
       "hlgndjlslygkdzpzxjyyzlpcxszfgwyydlyhcljscmbjhblyjlycblydpdqysxktbytdkdxjyycnrjmfdjgklccjbctbjddbblblcdqrp" +
       "pxjcglzcshltoljnmdddlngkaqakgjgyhheznmshrphqqjchgmfprxcjgdychghlyrzqlcngjnzsqdkqjymszswlcfqjqxgbggxmdjwlm" +
       "crnfkkfsyyljbmqammmycctbshcptxxzzsmphfshmclmldjfyqxsdyjdjjzzhqpdszglssjbckbxyqzjsgpsxjzqznqtbdkwxjkhhgflb" +
       "csmdldgdzdblzkycqnncsybzbfglzzxswmsccmqnjqsbdqsjtxxmbldxcclzshzcxrqjgjylxzfjphymzqqydfqjjlcznzjcdgzygcdxm" +
       "zysctlkphtxhtlbjxjlxscdqccbbqjfqzfsltjbtkqbsxjjljchczdbzjdczjccprnlqcgpfczlclcxzdmxmphgsgzgszzqjxlwtjpfsy" +
       "aslcjbtckwcwmytcsjjljcqlwzmalbxyfbpnlschtgjwejjxxglljstgshjqlzfkcgnndszfdeqfhbsaqdgylbxmmygszldydjmjjrgbj" +
       "gkgdhgkblgkbdmbylxwcxyttybkmrjjzxqjbhlmhmjjzmqasldcyxyqdlqcafywyxqhz";    //二级字库汉字的拼音首字母，共3008个字母，是按字库中汉字的顺序排列
        #endregion

        /// <summary>
        /// 转换为首字母拼音助记码
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        static public string ConvertToSpellShotCode(string text)
        {
            int ISNNumber = 0; //存放要转换的字符的内码数值 
            int ISNSectionID = 0;//内码的区码  
            int locateID = 0;//位码  
            int index = 0;//汉字的拼音的首字母在py_second_py中的位置
            StringBuilder sb_py = new StringBuilder(100);    //存放生成的助记码，只产生最多50个字母的助记码，也可以调整个数
            byte[] array = new byte[2];     //存放字符的内码
            for (int i = 0; i < text.Length; i++)
            {
                array = System.Text.Encoding.Default.GetBytes(text[i].ToString());  //得到字符串的内码
                if (array.Length == 1)
                {
                    if ((array[0] >= 97) && (array[0] <= 122))  //小写字母的处理
                    {
                        sb_py.Append(text[i].ToString());
                    }
                    if ((array[0] >= 65) && (array[0] <= 90))   // 将大写字母转换为小写字母
                    {
                        ISNNumber = array[0] + 32;
                        sb_py.Append((Convert.ToChar(ISNNumber)).ToString());
                    }
                }   //如果不是字母或汉字就不生成助记码，而略过这个字符
                if (array.Length == 2)
                {
                    if ((array[0] >= 176) && (array[0] <= 215)) //一级汉字
                    {
                        if ((array[0] > 212) || ((array[0] == 212) && (array[1] >= 209)))  //'匝'
                            sb_py.Append("z");
                        else if ((array[0] > 209) || ((array[0] == 209) && (array[1] >= 185)))  //'压'
                            sb_py.Append("y");
                        else if ((array[0] > 206) || ((array[0] == 206) && (array[1] >= 244)))  //'昔'
                            sb_py.Append("x");
                        else if ((array[0] > 205) || ((array[0] == 205) && (array[1] >= 218)))   //'挖'
                            sb_py.Append("w");
                        else if ((array[0] > 203) || ((array[0] == 203) && (array[1] >= 250)))   //'塌'
                            sb_py.Append("t");
                        else if ((array[0] > 200) || ((array[0] == 200) && (array[1] >= 246)))   //'撒'
                            sb_py.Append("s");
                        else if ((array[0] == 200) && (array[1] >= 187))   //'然'
                            sb_py.Append("r");
                        else if ((array[0] > 198) || ((array[0] == 198) && (array[1] >= 218)))   //'期'
                            sb_py.Append("q");
                        else if ((array[0] > 197) || ((array[0] == 197) && (array[1] >= 190)))  //'啪'
                            sb_py.Append("p");
                        else if ((array[0] == 197) && (array[1] >= 182))  //'哦'
                            sb_py.Append("o");
                        else if ((array[0] > 196) || ((array[0] == 196) && (array[1] >= 195)))  //'拿'
                            sb_py.Append("n");
                        else if ((array[0] > 194) || ((array[0] == 194) && (array[1] >= 232)))  //'妈'
                            sb_py.Append("m");
                        else if ((array[0] > 192) || ((array[0] == 192) && (array[1] >= 172)))  //'垃'
                            sb_py.Append("l");
                        else if ((array[0] > 191) || ((array[0] == 191) && (array[1] >= 166)))  //'喀'
                            sb_py.Append("k");
                        else if ((array[0] > 187) || ((array[0] == 187) && (array[1] >= 247)))  //'击'
                            sb_py.Append("j");
                        else if ((array[0] > 185) || ((array[0] == 185) && (array[1] == 254)))  //'哈'
                            sb_py.Append("h");
                        else if ((array[0] > 184) || ((array[0] == 184) && (array[1] >= 193)))  //'噶'
                            sb_py.Append("g");
                        else if ((array[0] > 183) || ((array[0] == 183) && (array[1] >= 162)))  //'发'
                            sb_py.Append("f");
                        else if ((array[0] > 182) || ((array[0] == 182) && (array[1] >= 234)))  //'蛾'
                            sb_py.Append("e");
                        else if ((array[0] > 180) || ((array[0] == 180) && (array[1] >= 238)))  //'搭'
                            sb_py.Append("d");
                        else if ((array[0] > 178) || ((array[0] == 178) && (array[1] >= 193)))  //'擦'
                            sb_py.Append("c");
                        else if ((array[0] > 176) || ((array[0] == 176) && (array[1] >= 197)))  //'芭'
                            sb_py.Append("b");
                        else if (array[0] == 176)  //'啊'
                            sb_py.Append("a");
                    }
                    else if ((array[0] >= 216) && (array[0] <= 247)) //二级汉字
                    {
                        ISNSectionID = array[0];  //读出内码区码
                        locateID = array[1];  //读出位码
                        ISNSectionID = ISNSectionID - 216;  //内码区码是从216开始
                        locateID = locateID - 161;   //内码位码是从161开始
                        if (locateID < 0)    //非一级与二级字库汉字，用空格代替
                        {
                            sb_py.Append(" ");
                        }
                        else
                        {
                            index = ISNSectionID * 94 + locateID;   //重点，是计算汉字的首字母在数组中的位置，因为每区是94个汉字
                            sb_py.Append(py_second_py[index].ToString());
                        }
                    }
                }
                //一个字符处理完
                if (sb_py.Length == 100)
                    break;  //如果助记码字母数达到100个，sb_py已用完，则退出循环
            } //for循环结束
            return sb_py.ToString();  //返回生成的助记码
        }



    }
}
